package com.tedu.springmvc2.controller;

import com.tedu.springmvc2.mapper.UserMapper;
import com.tedu.springmvc2.pojo.dto.UserDTO;
import com.tedu.springmvc2.pojo.dto.UserUpdateDTO;
import com.tedu.springmvc2.pojo.entity.User;
import com.tedu.springmvc2.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import java.util.Date;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/reg")
    public String reg(UserDTO userDTO){
        System.out.println("处理");
        System.out.println(userDTO);
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        user.setCreated(new Date());
        int num = userMapper.insert(user);
        System.out.println(user);
        return "成功";
    }
    @RequestMapping("/update")
    public String update(UserUpdateDTO userUpdateDTO){
        System.out.println("操作"+userUpdateDTO);
        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO, user);
        System.out.println(user);
        int num = userMapper.updateById(user);
        return num>0?"成功":"失败";
    }
    @RequestMapping("/delete")
    public String delete(Integer id){
        System.out.println("操作"+id);
        int num = userMapper.deleteById(id);
        return num>0?"成功":"失败";
    }

    @RequestMapping("/finAll")
    public List<UserVO> finAll(){
        return userMapper.selectAll();
    }
}
